var location = require("../../../mybehaviors/location/location");
const app = getApp();
const skinBehavior = require('../../../utils/themeSkin/skinBehavior');
Page({

  behaviors: [location,skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    page:1,
    total_page:1,
    search_type: 0,
    limit_1km:0,
    category_id: "",
    goods_info: {},
    imgHost: app.globalData.imgPath,
    button_group: [],
    parms: {
      1: {
        sort_by: 1,
        order_by: 0
      },
      2: {
        sort_by: 2,
        order_by: 0
      },
      3: {
        sort_by: 3,
        order_by: 0
      }
    },
    cursor: 1,
    point: {},
    citycode: 0,
    sort_by: 0,
    distance_max: 0,
    city_name: "定位",
    options: {},
    list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.options = options;
    this.getCategory();
    this.init_loaction();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成`
   */
  onReady() {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },
  init_loaction(){
    let address_point = wx.getStorageSync("address-point");
    if (this.data.options.cityId) { 
      wx.setStorageSync('local_life_code', this.data.options.cityId)
      this.setData({
        city_name: this.data.options.city
      });
      if (address_point) {
        this.data.point.lng = address_point.point.lng;
        this.data.point.lat = address_point.point.lat;
      }
      this.data.citycode = this.data.options.cityId;
    } else if (address_point) {
      wx.setStorageSync('local_life_code', address_point.adcode)
      let city_name = address_point.city
      this.setData({
        city_name
      });
      this.data.citycode = address_point.adcode;
      this.data.point.lng = address_point.point.lng;
      this.data.point.lat = address_point.point.lat;
    } 

    this.getData();
  },

  getLocationInfo() {
    try {
      this._getLocation((mapdata, e) => { //获取定位的城市，拿到定位的信息
        console.log(mapdata,'mapdata',e,'e');
        wx.setStorageSync('local_life_code', mapdata.adcode);
        let city_name = mapdata.address_component.city || '';
        this.setData({
          city_name
        });
        this.data.point.lng = e.lng;
        this.data.point.lat = e.lat;
        this.data.citycode = mapdata.adcode;
        this.getData();
      });
    } catch (e) {
      this.getData();
    }
  },
  navHome: function () {
    wx.navigateTo({
      url: '/packageI/citypickerPage/citypickerPage?plu=local_life&path=index'
    })
  },
  change(e) {
    let type = e.currentTarget.dataset.num;
    let order_by = '';
    if (this.data.parms[type].order_by == 0) {
      order_by = 1;
    } else if (this.data.parms[type].order_by == 1 && type == this.data.sort_by) {
      order_by = 2;
    } else {
      order_by = 1;
    }
    this.data.parms[type].order_by = order_by;
    this.data.search_type = (type*1) + 1;
    this.setData({
      list: [],
      sort_by: type,
      parms: this.data.parms
    });
    this.getData();
  },
  chunk(arr, n) {
    var result = [];
    for (var i = 0, len = arr.length; i < len; i += n) {
      result.push(arr.slice(i, i + n));
    }
    return result;
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.page>=this.data.total_page) {
      return;
    }
    let page = this.data.page + 1;
    this.getData(page);

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  goto() {
    let urlStr = "";
    if(this.data.options.city){
      urlStr ='/mircoApp/local_life/search/search?city=' + this.data.options.city + '&cityId=' + this.data.options.cityId;
    }else{
      urlStr = '/mircoApp/local_life/search/search?city='+this.data.city_name
    }
    wx.navigateTo({url:urlStr});
  },
  getData(page = 1) {
    let json = {limit_1km:this.data.limit_1km};
    if (this.data.search_type && this.data.search_type !== 1) {
      let order_by = this.data.parms[this.data.sort_by].order_by;
      json.search_direct = order_by == 1 ? 'asc' : 'desc';
    }
    if (this.data.search_type) {
      json.search_type = this.data.search_type;
    }
    let address_point = wx.getStorageSync("address-point");
    if (this.data.point.lat || (address_point && this.data.city_name == address_point.city)) {
      let latitude = '';
      let longitude = '';
      if (this.data.city_name == address_point.city) {
        latitude = address_point.point.lat * 1;
        longitude = address_point.point.lng * 1;
      } else {
        latitude = this.data.point.lat * 1;
        longitude = this.data.point.lng * 1;
      }
      json.latitude = latitude;
      json.longitude = longitude;
    }

    if (this.data.citycode) {
      let city_id = (this.data.options.city?(wx.getStorageSync('local_life_code')||this.data.options.cityId):this.data.citycode)+'';
      let _city = city_id.slice(0,-2) + '00';//市的city_id
      json.city_id = _city
    }
    let _json = {
      page,
      'search': json
    }
    app._postNetWork({
      url: app.getNetAddresss('plugin.local-life.api.goods.index'),
      data: _json,
      success: resdata => {
        let {
          result,
          data,
          msg
        } = resdata.data;
        if (result) {
         this.data.page = data.current_page;
         this.data.total_page = data.last_page;
          if (data.data && data.data.length !== 0) {
            let list = []
            if(data.current_page==1){
              list = data.data;
            }else{
              list = this.data.list.concat(data.data);
            }
            this.setData({list})
          }
        }else{
          app.tips(msg);
        }
        // if(this.data.point.lng==''){
        //   app.globalData.isLocationIng = false;
        //   this.getLocationInfo();
        // }
      }
    })
  },
  getCategory() {
    app._postNetWork({
      url: app.getNetAddresss('plugin.local-life.api.category.index'),
      success: resdata => {
        let {
          result,
          data,
          msg
        } = resdata.data;
        if (result) {
          let button_group = this.chunk(data.cate, 10);
          this.setData({
            button_group
          })
        }else{
          app.tips(msg);
        }
      }
    })
  },
  distance() {
    let limit_1km = this.data.limit_1km == 0 ? 1 : 0;
    this.setData({
      list: [],
      search_type:1,
      limit_1km
    })
    this.getData();
  },
  open_dialog(e) {
    let data = e.detail;
    let urlStr = "";
    if(this.data.options.city){
      urlStr =`/mircoApp/local_life/search/search?id=${data.id}&city= ${this.data.options.city}&cityId=${this.data.options.cityId}`
    }else{
      urlStr =`/mircoApp/local_life/search/search?id=${data.id}&city= ${this.data.city_name}&cityId=${this.data.citycode}`
    }
    wx.navigateTo({url:urlStr});
  },
})